Documentation link: https://plotly.com/python/plotly-express/
import plotly.express as px
import pandas as pd
df = px.data.iris()
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
marginal_y="violin", marginal_x="box", trendline="ols", template="simple_white")
fig.show()
df["e"] = df["sepal_width"]/100
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="species", error_x="e", error_y="e")
fig.show()
df = px.data.tips()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group")
fig.show()
df = px.data.medals_long()
df.head()
| nation | medal | count | |
|---|---|---|---|
| 0 | South Korea | gold | 24 |
| 1 | China | gold | 10 |
| 2 | Canada | gold | 9 |
| 3 | South Korea | silver | 13 |
| 4 | China | silver | 15 |
fig = px.bar(df, x="medal", y="count", color="nation",
pattern_shape="nation", pattern_shape_sequence=[".", "x", "+"])
fig.show()
df = px.data.tips()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.bar(df, x='sex', y='total_bill', color='smoker', barmode='group',
facet_row='time', facet_col='day',
category_orders={'day':['Thur','Fri','Sat','Sun'], 'time':['Lunch','Dinner']})
fig.show()
df = px.data.iris()
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
fig = px.scatter_matrix(df, dimensions=['sepal_width','sepal_length','petal_width','petal_length'],
color='species')
fig.show()
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
# creating labels
iris_labels = {"species_id": "Species",
"sepal_width": "Sepal Width", "sepal_length": "Sepal Length",
"petal_width": "Petal Width", "petal_length": "Petal Length", }
# using a color
tealrose_color = px.colors.diverging.Tealrose
fig = px.parallel_coordinates(df, color='species_id', labels=iris_labels,
color_continuous_scale=tealrose_color, color_continuous_midpoint=2)
fig.show()
df = px.data.tips()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
inferno_color = px.colors.sequential.Inferno
fig = px.parallel_categories(df, color='size', color_continuous_scale=inferno_color)
fig.show()
df = px.data.gapminder()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
df2007 = df.query('year==2007')
df2007.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
| 23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
| 47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
| 59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |
fig = px.scatter(df2007, x='gdpPercap', y='lifeExp', size='pop', color='continent',
hover_name='country' ,log_x=True, size_max=50)
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
fig = px.scatter(df, x='gdpPercap', y='lifeExp', size='pop', color='continent',
hover_name='country', facet_col='continent', log_x=True, size_max=40,
animation_frame='year', animation_group='country',
range_x=[100,15000], range_y=[25,90])
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
fig = px.line(df, x='year', y='lifeExp', color='continent',
line_group='country', hover_name='country',
line_shape='spline', render_mode='svg')
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
fig = px.area(df, x='year', y='pop', color='continent', line_group='country')
fig.show()
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])
df
| Task | Start | Finish | Resource | |
|---|---|---|---|---|
| 0 | Job A | 2009-01-01 | 2009-02-28 | Alex |
| 1 | Job B | 2009-03-05 | 2009-04-15 | Alex |
| 2 | Job C | 2009-02-20 | 2009-05-30 | Max |
fig = px.timeline(df, x_start='Start', x_end='Finish',
y='Resource',color='Task')
fig.show()
df_funnel = dict(number=[39, 27.4, 20.6, 11, 2],
stage=["Website visit", "Downloads", "Potential customers", "Requested price", "Invoice sent"])
print(df_funnel)
display(pd.DataFrame(df_funnel))
{'number': [39, 27.4, 20.6, 11, 2], 'stage': ['Website visit', 'Downloads', 'Potential customers', 'Requested price', 'Invoice sent']}
| number | stage | |
|---|---|---|
| 0 | 39.0 | Website visit |
| 1 | 27.4 | Downloads |
| 2 | 20.6 | Potential customers |
| 3 | 11.0 | Requested price |
| 4 | 2.0 | Invoice sent |
# here we are using dict instead of dataframe
fig = px.funnel(df_funnel, x='number', y='stage')
fig.show()
df = px.data.gapminder().query('year==2007').query('continent=="Europe"')
df.loc[df['pop']<4e6, 'country'] = 'Other countries'
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 23 | Other countries | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 83 | Austria | Europe | 2007 | 79.829 | 8199783 | 36126.492700 | AUT | 40 |
| 119 | Belgium | Europe | 2007 | 79.441 | 10392226 | 33692.605080 | BEL | 56 |
| 155 | Bosnia and Herzegovina | Europe | 2007 | 74.852 | 4552198 | 7446.298803 | BIH | 70 |
| 191 | Bulgaria | Europe | 2007 | 73.005 | 7322858 | 10680.792820 | BGR | 100 |
fig = px.pie(df, values='pop', names='country',
title='Population of Europian continent')
fig.show()
df = px.data.gapminder().query('year==2007')
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
| 23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
| 47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
| 59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |
fig = px.sunburst(df, path=['continent', 'country'],
values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
| 23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
| 47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
| 59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |
fig = px.treemap(df, path=[px.Constant('World'),'continent','country'],
values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()
df.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 11 | Afghanistan | Asia | 2007 | 43.828 | 31889923 | 974.580338 | AFG | 4 |
| 23 | Albania | Europe | 2007 | 76.423 | 3600523 | 5937.029526 | ALB | 8 |
| 35 | Algeria | Africa | 2007 | 72.301 | 33333216 | 6223.367465 | DZA | 12 |
| 47 | Angola | Africa | 2007 | 42.731 | 12420476 | 4797.231267 | AGO | 24 |
| 59 | Argentina | Americas | 2007 | 75.320 | 40301927 | 12779.379640 | ARG | 32 |
fig = px.icicle(df, path=[px.Constant('World'), 'continent', 'country'],
values='pop', color='lifeExp', hover_data=['iso_alpha'])
fig.show()
df = px.data.tips()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.histogram(df, x='total_bill', y='tip', color='sex')
fig.show()
fig = px.histogram(df, x='total_bill', y='tip', color='sex',
marginal='rug', hover_data=df.columns)
fig.show()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.box(df, x='day', y='total_bill', color='smoker', notched=True)
fig.show()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.violin(df, y='tip', x='smoker', color='sex',
box=True, points='all', hover_data=df.columns)
fig.show()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.ecdf(df, x='total_bill', color='sex')
fig.show()
df.head()
| total_bill | tip | sex | smoker | day | time | size | |
|---|---|---|---|---|---|---|---|
| 0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
| 1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
| 2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
| 3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
| 4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
fig = px.strip(df, x='total_bill', y='time', orientation='h', color='smoker')
fig.show()
df = px.data.iris()
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
fig = px.density_contour(df, x='sepal_width', y='sepal_length')
fig.show()
df.head()
| sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
fig = px.density_heatmap(df, x='sepal_width', y='sepal_length',
marginal_x='rug', marginal_y='histogram')
fig.show()
data1 = [[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
data1
[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data1,
labels=dict(x='day of week',y='time of day',color='Productivity'),
x=['Mon','Tue','Wed','Thu','Fri'],
y=['Morning','Afternoon','Evening'])
fig.update_xaxes(side='top')
fig.show()
from skimage import io
img = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
fig = px.imshow(img)
fig.show()
The maps section is skipped.
df = px.data.wind()
df.head()
| direction | strength | frequency | |
|---|---|---|---|
| 0 | N | 0-1 | 0.5 |
| 1 | NNE | 0-1 | 0.6 |
| 2 | NE | 0-1 | 0.5 |
| 3 | ENE | 0-1 | 0.4 |
| 4 | E | 0-1 | 0.4 |
plasma_r_col = px.colors.sequential.Plasma_r
fig = px.scatter_polar(df, r='frequency', theta='direction', color='strength',
symbol='strength', color_discrete_sequence=plasma_r_col)
fig.show()
df.head()
| direction | strength | frequency | |
|---|---|---|---|
| 0 | N | 0-1 | 0.5 |
| 1 | NNE | 0-1 | 0.6 |
| 2 | NE | 0-1 | 0.5 |
| 3 | ENE | 0-1 | 0.4 |
| 4 | E | 0-1 | 0.4 |
fig = px.line_polar(df, r='frequency', theta='direction', color='strength',
line_close=True, color_discrete_sequence=plasma_r_col)
fig.show()
C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead. C:\ProgramData\Anaconda3\lib\site-packages\plotly\express\_core.py:271: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
df.head()
| direction | strength | frequency | |
|---|---|---|---|
| 0 | N | 0-1 | 0.5 |
| 1 | NNE | 0-1 | 0.6 |
| 2 | NE | 0-1 | 0.5 |
| 3 | ENE | 0-1 | 0.4 |
| 4 | E | 0-1 | 0.4 |
fig = px.bar_polar(df, r='frequency', theta='direction', color='strength',
template='plotly_dark', color_discrete_sequence=plasma_r_col)
fig.show()
df = px.data.election()
df.head()
| district | Coderre | Bergeron | Joly | total | winner | result | district_id | |
|---|---|---|---|---|---|---|---|---|
| 0 | 101-Bois-de-Liesse | 2481 | 1829 | 3024 | 7334 | Joly | plurality | 101 |
| 1 | 102-Cap-Saint-Jacques | 2525 | 1163 | 2675 | 6363 | Joly | plurality | 102 |
| 2 | 11-Sault-au-Récollet | 3348 | 2770 | 2532 | 8650 | Coderre | plurality | 11 |
| 3 | 111-Mile-End | 1734 | 4782 | 2514 | 9030 | Bergeron | majority | 111 |
| 4 | 112-DeLorimier | 1770 | 5933 | 3044 | 10747 | Bergeron | majority | 112 |
colorsmap = {'Joly':'blue', 'Bergeron':'green', 'Coderre':'red'}
fig = px.scatter_3d(df, x='Joly', y='Coderre', z='Bergeron', color='winner',
size='total', hover_name='district', symbol='result',
color_discrete_map=colorsmap)
fig.show()
df.head()
| district | Coderre | Bergeron | Joly | total | winner | result | district_id | |
|---|---|---|---|---|---|---|---|---|
| 0 | 101-Bois-de-Liesse | 2481 | 1829 | 3024 | 7334 | Joly | plurality | 101 |
| 1 | 102-Cap-Saint-Jacques | 2525 | 1163 | 2675 | 6363 | Joly | plurality | 102 |
| 2 | 11-Sault-au-Récollet | 3348 | 2770 | 2532 | 8650 | Coderre | plurality | 11 |
| 3 | 111-Mile-End | 1734 | 4782 | 2514 | 9030 | Bergeron | majority | 111 |
| 4 | 112-DeLorimier | 1770 | 5933 | 3044 | 10747 | Bergeron | majority | 112 |
fig = px.scatter_ternary(df, a='Joly', b='Coderre', c='Bergeron', color='winner',
size='total', hover_name='district', size_max=12, color_discrete_map=colorsmap)
fig.show()
import plotly.graph_objects as go
fig = go.Figure()
# from dash import Dash, dcc, html